home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH11 / SRC / DEPTH2.FRM < prev    next >
Text File  |  1997-01-17  |  10KB  |  395 lines

  1. VERSION 4.00
  2. Begin VB.Form DepthSortForm 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "Depth Sort"
  6.    ClientHeight    =   5685
  7.    ClientLeft      =   1230
  8.    ClientTop       =   870
  9.    ClientWidth     =   6030
  10.    BeginProperty Font 
  11.       name            =   "MS Sans Serif"
  12.       charset         =   1
  13.       weight          =   700
  14.       size            =   8.25
  15.       underline       =   0   'False
  16.       italic          =   0   'False
  17.       strikethrough   =   0   'False
  18.    EndProperty
  19.    ForeColor       =   &H80000008&
  20.    Height          =   6375
  21.    KeyPreview      =   -1  'True
  22.    Left            =   1170
  23.    LinkTopic       =   "Form1"
  24.    ScaleHeight     =   5685
  25.    ScaleWidth      =   6030
  26.    Top             =   240
  27.    Width           =   6150
  28.    Begin VB.TextBox PhiText 
  29.       Height          =   285
  30.       Left            =   3600
  31.       TabIndex        =   6
  32.       Text            =   "0.7854"
  33.       Top             =   5400
  34.       Width           =   855
  35.    End
  36.    Begin VB.TextBox ThetaText 
  37.       Height          =   285
  38.       Left            =   2040
  39.       TabIndex        =   4
  40.       Text            =   "25.7611"
  41.       Top             =   5400
  42.       Width           =   855
  43.    End
  44.    Begin VB.TextBox RText 
  45.       Height          =   285
  46.       Left            =   480
  47.       TabIndex        =   2
  48.       Text            =   "10.0000"
  49.       Top             =   5400
  50.       Width           =   855
  51.    End
  52.    Begin VB.PictureBox Pict 
  53.       AutoRedraw      =   -1  'True
  54.       BackColor       =   &H00C0C0C0&
  55.       Height          =   5295
  56.       Left            =   0
  57.       ScaleHeight     =   -14
  58.       ScaleLeft       =   -7
  59.       ScaleMode       =   0  'User
  60.       ScaleTop        =   7
  61.       ScaleWidth      =   15.926
  62.       TabIndex        =   0
  63.       Top             =   0
  64.       Width           =   6015
  65.    End
  66.    Begin MSComDlg.CommonDialog LoadDialog 
  67.       Left            =   4920
  68.       Top             =   5280
  69.       _version        =   65536
  70.       _extentx        =   847
  71.       _extenty        =   847
  72.       _stockprops     =   0
  73.    End
  74.    Begin VB.Label Label1 
  75.       Caption         =   "Phi"
  76.       Height          =   255
  77.       Index           =   2
  78.       Left            =   3240
  79.       TabIndex        =   5
  80.       Top             =   5400
  81.       Width           =   375
  82.    End
  83.    Begin VB.Label Label1 
  84.       Caption         =   "Theta"
  85.       Height          =   255
  86.       Index           =   1
  87.       Left            =   1440
  88.       TabIndex        =   3
  89.       Top             =   5400
  90.       Width           =   495
  91.    End
  92.    Begin VB.Label Label1 
  93.       Caption         =   "R"
  94.       Height          =   255
  95.       Index           =   0
  96.       Left            =   240
  97.       TabIndex        =   1
  98.       Top             =   5400
  99.       Width           =   255
  100.    End
  101.    Begin VB.Menu mnuFile 
  102.       Caption         =   "&File"
  103.       Begin VB.Menu mnuFileLoad 
  104.          Caption         =   "&Load..."
  105.          Shortcut        =   ^L
  106.       End
  107.       Begin VB.Menu mnuFileSep 
  108.          Caption         =   "-"
  109.       End
  110.       Begin VB.Menu mnuFileExit 
  111.          Caption         =   "E&xit"
  112.       End
  113.    End
  114. End
  115. Attribute VB_Name = "DepthSortForm"
  116. Attribute VB_Creatable = False
  117. Attribute VB_Exposed = False
  118.  
  119. Option Explicit
  120.  
  121. ' Location of viewing eye.
  122. Dim EyeR As Single
  123. Dim EyeTheta As Single
  124. Dim EyePhi As Single
  125.  
  126. Const Dtheta = PI / 20
  127. Const Dphi = PI / 20
  128. Const Dr = 1
  129.  
  130. ' Location of focus point.
  131. Const FocusX = 0#
  132. Const FocusY = 0#
  133. Const FocusZ = 0#
  134.  
  135. Dim Projector(1 To 4, 1 To 4) As Single
  136.  
  137. Dim ThePicture As ObjPicture
  138.  
  139. Dim ShowingParameters As Boolean
  140.  
  141. ' *******************************************************
  142. ' Rotate the points in the cube and draw the cube.
  143. ' *******************************************************
  144. Private Sub DrawData(pic As Object)
  145. Dim x As Single
  146. Dim y As Single
  147. Dim z As Single
  148. Dim edge_pen As Long
  149. Dim old_pen As Long
  150. Dim fill_brush As Long
  151. Dim old_brush As Long
  152. Dim status As Long
  153. Dim t1(1 To 4, 1 To 4) As Single
  154. Dim t2(1 To 4, 1 To 4) As Single
  155. Dim T12(1 To 4, 1 To 4) As Single
  156. Dim T123(1 To 4, 1 To 4) As Single
  157. Dim pt As Point3D
  158.  
  159.     MousePointer = vbHourglass
  160.     
  161.     ' Prevent overflow errors when drawing lines
  162.     ' too far out of bounds.
  163.     On Error Resume Next
  164.  
  165.     ' Cull backfaces.
  166.     ThePicture.Culled = False
  167.     m3SphericalToCartesian EyeR, EyeTheta, EyePhi, x, y, z
  168.     ThePicture.Cull x, y, z
  169.     
  170.     ' Clip faces behind the center of projection.
  171.     ThePicture.ClipEye EyeR
  172.     
  173.     ' Transform coordinates into pixels.
  174.     m3Scale t1, _
  175.         Pict.ScaleX(1, Pict.ScaleMode, vbPixels), _
  176.         Pict.ScaleY(1, Pict.ScaleMode, vbPixels), _
  177.         1
  178.     m3Translate t2, _
  179.         -Pict.ScaleX(Pict.ScaleLeft, Pict.ScaleMode, vbPixels), _
  180.         -Pict.ScaleY(Pict.ScaleTop, Pict.ScaleMode, vbPixels), _
  181.         0
  182.     m3MatMultiply T12, t1, t2
  183.     m3MatMultiplyFull T123, Projector, T12
  184.     
  185.     ' Transform the points.
  186.     ThePicture.ApplyFull T123
  187.  
  188.     ' Clear the screen. We must do this before
  189.     ' selecting the pen and brush since Cls resets
  190.     ' the pen and brush to default values.
  191.     pic.Cls
  192.     
  193.     ' Get a pen and brush.
  194.     edge_pen = CreatePen(PS_SOLID, pic.DrawWidth, pic.ForeColor)
  195.     old_pen = SelectObject(pic.hdc, edge_pen)
  196.     fill_brush = CreateSolidBrush(pic.BackColor)
  197.     old_brush = SelectObject(pic.hdc, fill_brush)
  198.  
  199.     ' Display the data.
  200.     ThePicture.DrawOrdered pic, EyeR
  201.     
  202.     pic.Refresh
  203.  
  204.     ' Restore the old pen and brush.
  205.     edge_pen = SelectObject(pic.hdc, old_pen)
  206.     fill_brush = SelectObject(pic.hdc, old_brush)
  207.     status = DeleteObject(edge_pen)
  208.     status = DeleteObject(fill_brush)
  209.     
  210.     ' Display the viewing parameters.
  211.     ShowViewingParameters
  212.  
  213.     MousePointer = vbDefault
  214. End Sub
  215.  
  216. Sub ShowViewingParameters()
  217.     ShowingParameters = True
  218.     
  219.     RText.Text = Format$(EyeR, "0.0000")
  220.     ThetaText.Text = Format$(EyeTheta, "0.0000")
  221.     PhiText.Text = Format$(EyePhi, "0.0000")
  222.     
  223.     RText.Refresh
  224.     ThetaText.Refresh
  225.     PhiText.Refresh
  226.  
  227.     ShowingParameters = False
  228. End Sub
  229.  
  230.  
  231.  
  232. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  233.     Select Case KeyCode
  234.         Case vbKeyLeft
  235.             EyeTheta = EyeTheta - Dtheta
  236.         
  237.         Case vbKeyRight
  238.             EyeTheta = EyeTheta + Dtheta
  239.         
  240.         Case vbKeyUp
  241.             EyePhi = EyePhi - Dphi
  242.         
  243.         Case vbKeyDown
  244.             EyePhi = EyePhi + Dphi
  245.                 
  246.         Case Else
  247.             Exit Sub
  248.     End Select
  249.  
  250.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  251.     DrawData Pict
  252. End Sub
  253.  
  254.  
  255. Private Sub Form_KeyPress(KeyAscii As Integer)
  256.     Select Case KeyAscii
  257.         Case Asc("+")
  258.             EyeR = EyeR + Dr
  259.         
  260.         Case Asc("-")
  261.             EyeR = EyeR - Dr
  262.         
  263.         Case Else
  264.             Exit Sub
  265.     End Select
  266.  
  267.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  268.     DrawData Pict
  269. End Sub
  270.  
  271. Private Sub Form_Load()
  272.     ' Initialize the eye position.
  273.     EyeR = 20
  274.     EyeTheta = PI * 0.2
  275.     EyePhi = PI * 0.05
  276.     
  277.     ' Initialize the projection transformation.
  278.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  279.     
  280.     ' Create the data.
  281.     CreateData
  282.  
  283.     ' Project and draw the data.
  284.     DrawData Pict
  285. End Sub
  286.  
  287. ' ***********************************************
  288. ' Create some axes initially.
  289. ' ***********************************************
  290. Sub CreateData()
  291. Dim pline As ObjPolyline
  292.  
  293.     Set ThePicture = New ObjPicture
  294.     Set pline = New ObjPolyline
  295.     ThePicture.Objects.Add pline
  296.  
  297.     pline.AddSegment 0, 0, 0, 5, 0, 0
  298.     pline.AddSegment 0, 0, 0, 0, 5, 0
  299.     pline.AddSegment 0, 0, 0, 0, 0, 5
  300. End Sub
  301. Private Sub mnuFileExit_Click()
  302.     Unload Me
  303. End Sub
  304.  
  305.  
  306. Private Sub mnuFileLoad_Click()
  307. Dim fname As String
  308. Dim filenum As Integer
  309. Dim txt As String
  310. Dim xmin As Single
  311. Dim ymin As Single
  312. Dim xmax As Single
  313. Dim ymax As Single
  314.  
  315.     ' Allow the user to pick a file.
  316.     On Error Resume Next
  317.     LoadDialog.filename = "*.APF"
  318.     LoadDialog.ShowOpen
  319.     LoadDialog.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  320.     If Err.Number = cdlCancel Then
  321.         Unload LoadDialog
  322.         Exit Sub
  323.     ElseIf Err.Number <> 0 Then
  324.         Unload LoadDialog
  325.         Beep
  326.         MsgBox "Error selecting file.", , vbExclamation
  327.         Exit Sub
  328.     End If
  329.     On Error GoTo 0
  330.     
  331.     fname = LoadDialog.filename
  332.     LoadDialog.InitDir = Left$(fname, Len(fname) _
  333.         - Len(LoadDialog.FileTitle) - 1)
  334.  
  335.     ' Clear the picture.
  336.     Set ThePicture = Nothing
  337.     
  338.     ' Open the file.
  339.     filenum = FreeFile
  340.     Open fname For Input As #filenum
  341.     
  342.     ' Make sure it's an Object Picture File.
  343.     Input #filenum, txt
  344.     If txt <> "3D APF PICTURE" Then
  345.         Close filenum
  346.         Caption = "Show APF"
  347.         Beep
  348.         MsgBox "Error reading file """ & fname & """.", , vbExclamation
  349.         Exit Sub
  350.     End If
  351.  
  352.     ' Read the picture.
  353.     Set ThePicture = New ObjPicture
  354.     ThePicture.FileInput filenum
  355.     
  356.     ' Close the file.
  357.     Close filenum
  358.  
  359.     Caption = "Show APF [" & LoadDialog.FileTitle & "]"
  360.  
  361.     ' Refresh the display.
  362.     DrawData Pict
  363. End Sub
  364.  
  365.  
  366.  
  367.  
  368. Private Sub PhiText_Change()
  369.     If ShowingParameters Then Exit Sub
  370.     EyePhi = CSng(PhiText.Text)
  371.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  372.     DrawData Pict
  373. End Sub
  374.  
  375.  
  376. Private Sub RText_Change()
  377.     If ShowingParameters Then Exit Sub
  378.     EyeR = CSng(RText.Text)
  379.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  380.     DrawData Pict
  381. End Sub
  382.  
  383. Private Sub ThetaText_Change()
  384.     If ShowingParameters Then Exit Sub
  385.     EyeTheta = CSng(ThetaText.Text)
  386.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  387.     DrawData Pict
  388. End Sub
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.